793A - Oleg and shares - CodeForces Solution


implementation math *900

Please click on ads to support us..

Python Code:

n, k = (int(i) for i in input().split())
a = [int(i) for i in input().split()]
mi = min(a)
res = 0
for i in a:
    d, r = divmod(i - mi, k)
    if r == 0:
        res += d
    else:
        res = -1
        break
print(res)

C++ Code:

#include <bits/stdc++.h>
#define ll long long
using namespace std;


int main (){
	
	ll n,k;
	cin>>n>>k;

	ll a[n];
	for(ll i=0; i<n; i++){
		cin>>a[i];
	}

	ll min = a[0];
	for(ll i=0; i<n; i++){
		if(a[i]<min) min=a[i];
	}

	ll count=0;
	bool flag=true;
	for(ll i=0; i<n; i++){

		if(a[i]==min) continue;

		else if((a[i]-min)%k==0){
			count=count+((a[i]-min)/k);
		}

		else{
			flag=false;
			break;
		}
	}

	if(flag==true) cout<<count;
	else cout<<-1;

	return 0;
}


Comments

Submit
0 Comments
More Questions

63A - Sinking Ship
1704B - Luke is a Foodie
298B - Sail
239A - Two Bags of Potatoes
1704E - Count Seconds
682A - Alyona and Numbers
44A - Indian Summer
1133C - Balanced Team
1704A - Two 0-1 Sequences
1467A - Wizard of Orz
1714E - Add Modulo 10
1714A - Everyone Loves to Sleep
764A - Taymyr is calling you
1714B - Remove Prefix
1264F - Beautiful Fibonacci Problem
52A - 123-sequence
1543A - Exciting Bets
1714D - Color with Occurrences
215B - Olympic Medal
1445A - Array Rearrangment
1351A - A+B (Trial Problem)
935B - Fafa and the Gates
1291A - Even But Not Even
1269A - Equation
441A - Valera and Antique Items
1702C - Train and Queries
816B - Karen and Coffee
838D - Airplane Arrangements
148B - Escape
847G - University Classes